package com.swt.gap.service.impl;

import com.swt.gap.common.FileTypeEnum;
import com.swt.gap.dao.FileDao;
import com.swt.gap.dao.TaskFileMapper;
import com.swt.gap.dao.TaskSubscribeDataDao;
import com.swt.gap.model.entity.File;
import com.swt.gap.model.entity.TaskFile;
import com.swt.gap.model.vo.*;
import com.swt.gap.service.TaskFileService;
import com.swt.gap.util.UserLoginUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author tyh
 * @Date 2020/4/23 23:55
 */

@Service
public class TaskFileServiceImpl implements TaskFileService {
    private final TaskFileMapper taskFileMapper;
    private final FileDao fileDao;
    private final TaskSubscribeDataDao taskSubscribeDataDao;

    public TaskFileServiceImpl(FileDao fileDao, TaskFileMapper taskFileMapper, TaskSubscribeDataDao taskSubscribeDataDao) {
        this.fileDao = fileDao;
        this.taskFileMapper = taskFileMapper;
        this.taskSubscribeDataDao = taskSubscribeDataDao;
    }


    @Override
    public List<TypeFileVO> getTypeFileVOList(Integer taskId) {

        //数据类型统计
        List<TypeFileVO> typeFiles = new ArrayList<>();
        TypeFileVO excelFiles = new TypeFileVO();
        excelFiles.setTypeName(FileTypeEnum.CSV.getDataTypeName());
        excelFiles.setTypeCode(FileTypeEnum.CSV.getDataTypeCode());
        int count1 = 0;
        TypeFileVO wordFiles = new TypeFileVO();
        wordFiles.setTypeName(FileTypeEnum.TXT.getDataTypeName());
        wordFiles.setTypeCode(FileTypeEnum.TXT.getDataTypeCode());
        int count2 = 0;
        TypeFileVO mediaFiles = new TypeFileVO();
        mediaFiles.setTypeName(FileTypeEnum.JPG.getDataTypeName());
        mediaFiles.setTypeCode(FileTypeEnum.JPG.getDataTypeCode());
        int count3 = 0;
        for (TaskFile taskFile : taskFileMapper.selectByTaskId(taskId)) {
            File file = fileDao.selectByPrimaryKey(taskFile.getFileId());
            if(FileTypeEnum.CSV.getDataTypeCode().equals(file.getFileType())){
                count1++;
            }else if(FileTypeEnum.TXT.getDataTypeCode().equals(file.getFileType())){
                    count2++;
            }else if(FileTypeEnum.JPG.getDataTypeCode().equals(file.getFileType())){
                count3++;
            }
        }
        excelFiles.setCount(count1);
        wordFiles.setCount(count2);
        mediaFiles.setCount(count3);
        typeFiles.add(excelFiles);
        typeFiles.add(wordFiles);
        typeFiles.add(mediaFiles);
        return typeFiles;
    }


    @Override
    public Integer addTaskFiles(Integer taskId, List<Integer> fileIds) {
        int result = 0;
        for (Integer fileId : fileIds) {
            TaskFile taskFile = new TaskFile();
            taskFile.setTaskId(taskId);
            taskFile.setFileId(fileId);
            taskFile.setCreatedTime(new Date());
            taskFile.setStatus(0);

            result += taskFileMapper.insert(taskFile);

            taskSubscribeDataDao.updateStatusByTaskIdAndFileId(taskId, fileId, 3);

        }
        return result;
    }

    @Override
    public Integer deleteTaskFile(Integer fileId, Integer taskId) {
        return taskFileMapper.deleteTaskFile(fileId, taskId);
    }

    @Override
    public  List<FileVO> getFile(String keyword) {
        Integer userId = UserLoginUtils.getUserId();
        List<FileVO> fileVOList = fileDao.getFile(userId, keyword);
        List<FileVO> fileVO = new ArrayList<>();
        List<String> stringList = new ArrayList<>();
        for (FileVO file : fileVOList) {
            //获取所有分类
            if (!stringList.contains(file.getClassName())) {
                stringList.add(file.getClassName());
                FileVO f = new FileVO();
                List<FileVO> children = new ArrayList<>();
                children.add(file);
                f.setClassName(file.getClassName());
                f.setFileClassId(file.getFileClassId());
                f.setChildren(children);
                fileVO.add(f);
            } else {
                for (FileVO cFile : fileVO) {
                    if (cFile.getClassName().equals(file.getClassName())) {
                        cFile.getChildren().add(file);
                    }
                }
            }
        }
        return fileVO;
    }
}
